package com.wutao.panzh.admin.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.SceneryItem;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.admin.content.controller.imageUpload.DropzoneController;
import com.wutao.panzh.service.SceneryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class SceneryItemController {

    @Reference
    private SceneryItemService sceneryItemService;

    @Autowired
    private DropzoneController dropzoneController;

    //执行分页的方法：
    @ResponseBody
    @RequestMapping("/scenery/get/page")
    public ResultVO<PageInfo<SceneryItem>> getSceneryItemPage(
            //由于pageNo在请求参数中不一定能够提供，所以设置默认值。
            //默认值为1表示未提供此数据时前往第1页
            @RequestParam(value="pageNo",defaultValue="1") Integer pageNo,
            //由于keyword在请求参数中不一定能够提供，所以设置默认值。
            //默认值为空字符串将不会对SQL语句有任何影响
            @RequestParam(value="keyword",defaultValue="") String keyword){

        PageInfo<SceneryItem> SceneryItemPageInfo = sceneryItemService.getSceneryItemPageInfo(pageNo, ProjectParames.PAGE_SIZE, keyword);

        return new ResultVO<PageInfo<SceneryItem>>(ResultVO.SUCCESS,ResultVO.NO_MSG ,SceneryItemPageInfo);
    }

    @RequestMapping("/get/scenery/by/id")
    @ResponseBody
    public ResultVO getSceneryById(@RequestParam("sceneryId") String sceneryId){
        SceneryItem sceneryItem = sceneryItemService.getSceneryItemById(sceneryId);
        //未在数据库中查询到数据；
        if (sceneryItem == null){
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_DOES_NOT_EXIST,ResultVO.NO_DATA);
        }else{
            //在数据库中查询到数据
            return new ResultVO<SceneryItem>(ResultVO.SUCCESS,ResultVO.NO_MSG ,sceneryItem);
        }
    }


    //删除InformationItem的方法；
    @RequestMapping("/delete/sceneryItme")
    @ResponseBody
    public ResultVO deleteSceneryById(@RequestParam("sceneryId") String sceneryId){
        //如果抛出异常，进行捕获后返回
        try {
            sceneryItemService.deleteSceneryById(sceneryId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DELETE_INFORMATION_EXCEPTION,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.DELETE_INFORMATION_SUCCESS,ResultVO.NO_DATA);
    }


    //保存InformationItem的方法；
    @ResponseBody
    @RequestMapping("/save/sceneryItme")
    public ResultVO<String> saveInformationItem(SceneryItem sceneryItem){
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复；
        String itemName = sceneryItem.getSceneryName();
        //调用方法获得同名的InformationItem；
        List<SceneryItem> list = sceneryItemService.getSceneryItemByItemName(itemName);
        //如果为null，说明没有该信息，但是如果查到了，就证明系统已经有重名的资讯，直接拒绝插入；
        if (list.size() > 0){
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS,ResultVO.NO_DATA);
        }
        //进行信息的插入；
        sceneryItemService.saveSceneryItem(sceneryItem);
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY,ResultVO.NO_DATA);
    }


    @ResponseBody
    @RequestMapping("/upate/sceneryItme")
    public ResultVO<String> updateInformationItem(SceneryItem sceneryItem){
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复，检查有没有同名的；
        String itemName = sceneryItem.getSceneryName();
        //调用方法获得同名的InformationItem,注意，本身可能存在数据库，可能会把本身查出来，所以必须要验证List的长度；
        List<SceneryItem> resultSceneryItems = sceneryItemService.getSceneryItemByItemName(itemName);
        //这里涉及一点小算法,只可能等于0或者等于1,如若size等于0，直接进行更新， 若size等于1，则比较id，若是id相同，则可以进行更新，若不同,拒绝更新
        if (resultSceneryItems.size() == 0 ){
            sceneryItemService.updateScenery(sceneryItem);
            return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY,ResultVO.NO_DATA);
        }else{
            SceneryItem resultScenery = resultSceneryItems.get(0);
            if (resultScenery.getId().compareTo(sceneryItem.getId()) == 0){
                sceneryItemService.updateScenery(sceneryItem);
                return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY,ResultVO.NO_DATA);
            }
        }
        return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS,ResultVO.NO_DATA);
    }

    //去往修改页面，必须携带数据；
    @RequestMapping("/update_scenery")
    public String toUpdateScenery(Model model, HttpServletRequest request){
        String itemId = request.getParameter("sceneryId");
        SceneryItem sceneryItem = sceneryItemService.getSceneryItemById(itemId);
        //放入数据域
        model.addAttribute("sceneryItem",sceneryItem);
        return "update_scenery";
    }

//    /upload/big/image/scenery
    @ResponseBody
    @RequestMapping("/upload/big/image/scenery")
    public ResultVO<String> uploadBigImageScenery(@RequestParam("file") MultipartFile image, @RequestParam("id") String sceneryItemId){

        try {
            Map<String, Object> imageMap = dropzoneController.upload(image);
            String imagSrc = (String) imageMap.get("fileName");
            sceneryItemService.insertSceneryImageSrc(imagSrc,sceneryItemId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DATA_SAVING_FAILED,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY,ResultVO.NO_DATA);
    }
}
